package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.admin.goods.bo.CreateBo;
import com.cskaoyan.bean.admin.goods.bo.UpadateBo;
import com.cskaoyan.bean.admin.goods.vo.CatAndBrandVo;
import com.cskaoyan.bean.admin.goods.vo.GoodsDetailVo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.admin.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.System;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname WxHomeServiceImpl
 * @Description
 * @Date 2021/8/14 15:27
 * @Author : xujun
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    GoodsAttributeMapper attributeMapper;

    @Autowired
    GoodsSpecificationMapper specificationMapper;

    @Autowired
    GoodsProductMapper productMapper;

    @Autowired
    BrandMapper brandMapper;

    @Override
    public BaseRespData query(String goodsSn, String name, BaseParamBo param) {

        PageHelper.startPage(param.getPage(), param.getLimit());

        GoodsExample example = new GoodsExample();
        example.setOrderByClause(param.getSort() + " " + param.getOrder());

        GoodsExample.Criteria criteria = example.createCriteria();
        // 因为是逻辑删除，所以已删除的不能再显示
        criteria.andDeletedEqualTo(false);
        if (goodsSn != null && !"".equals(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        List<Goods> goods = goodsMapper.selectByExampleWithBLOBs(example);

        PageInfo<Goods> pageInfo = new PageInfo<>(goods);
        long total = pageInfo.getTotal();

        return BaseRespData.create(goods, total);
    }

    @Override
    public void delete(Goods good) {

        good.setDeleted(true);

        int i = goodsMapper.updateByPrimaryKey(good);

    }

    @Override
    public GoodsDetailVo getDetail(Integer id) {

        // goods
        Goods goods = goodsMapper.selectByPrimaryKey(id);

        // specifications
        GoodsSpecificationExample specificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria criteria1 = specificationExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GoodsSpecification> specifications = specificationMapper.selectByExample(specificationExample);

        // products
        GoodsProductExample productExample = new GoodsProductExample();
        GoodsProductExample.Criteria criteria2 = productExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GoodsProduct> products = productMapper.selectByExample(productExample);

        // attributes
        GoodsAttributeExample attributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria criteria3  = attributeExample.createCriteria();
        criteria3.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GoodsAttribute> attributes = attributeMapper.selectByExample(attributeExample);

        // category的id,pid的list
        Category category = categoryMapper.selectByPrimaryKey(goods.getCategoryId());
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(category.getId());
        arrayList.add(category.getPid());

        // 封装 vo
        GoodsDetailVo vo = new GoodsDetailVo();
        vo.setGoods(goods);
        vo.setSpecifications(specifications);
        vo.setProducts(products);
        vo.setAttributes(attributes);
        vo.setCategoryIds(arrayList);

        return vo;

    }

    @Override
    public CatAndBrandVo catAndBrand() {

        // categoryList，嵌套循环遍历Category表，先封装父类目信息，再匹配子类目并封装子类目信息
        ArrayList<CatAndBrandVo.CategoryInnerVo> categoryList = new ArrayList<>(); // 父类目list
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            // 是父类目的话，先封装父类目
            if (category.getPid() == 0) {
                CatAndBrandVo.CategoryInnerVo categoryInnerVo = new CatAndBrandVo.CategoryInnerVo();
                categoryInnerVo.setValue(category.getId());
                categoryInnerVo.setLabel(category.getName());
                // 再处理子类目
                Integer parentId = category.getId();
                // 子类目list
                ArrayList<CatAndBrandVo.CategoryInnerVo.ChildrenVo> childrenList = new ArrayList<>();
                for (Category children : categories) {
                    if (children.getPid().equals(parentId) && children.getDeleted().equals(false)) {
                        CatAndBrandVo.CategoryInnerVo.ChildrenVo childrenVo = new CatAndBrandVo.CategoryInnerVo.ChildrenVo();
                        childrenVo.setLabel(children.getName());
                        childrenVo.setValue(children.getId());
                        childrenList.add(childrenVo);
                    }
                }
                categoryInnerVo.setChildren(childrenList);

                categoryList.add(categoryInnerVo);
            }
        }

        // brandList
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        ArrayList<CatAndBrandVo.BrandInnerVo> brandInnerVos = new ArrayList<>();
        for (Brand brand : brands) {

            CatAndBrandVo.BrandInnerVo brandInnerVo = new CatAndBrandVo.BrandInnerVo();
            brandInnerVo.setLabel(brand.getName());
            brandInnerVo.setValue(brand.getId());

            brandInnerVos.add(brandInnerVo);
        }

        // 封装 categoryList 和 brandList 进 vo
        CatAndBrandVo catAndBrandVo = new CatAndBrandVo();
        catAndBrandVo.setBrandList(brandInnerVos);
        catAndBrandVo.setCategoryList(categoryList);

        return catAndBrandVo;
    }

    @Override
    public void update(UpadateBo bo) {

        // TODO 逻辑不够完善

        Date date = new Date(System.currentTimeMillis());

        // goods
        Goods goods = bo.getGoods();
        goods.setUpdateTime(date);
        goodsMapper.updateByPrimaryKeySelective(goods);

        // attributes
        List<GoodsAttribute> attributes = bo.getAttributes();
        for (GoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(date);
            attributeMapper.updateByPrimaryKeySelective(attribute);
        }

        // products
        List<GoodsProduct> products = bo.getProducts();
        for (GoodsProduct product : products) {
            product.setUpdateTime(date);
            productMapper.updateByPrimaryKeySelective(product);
        }

        // specifications
        List<GoodsSpecification> specifications = bo.getSpecifications();
        for (GoodsSpecification specification : specifications) {
            specification.setUpdateTime(date);
            specificationMapper.updateByPrimaryKeySelective(specification);
        }

    }

    @Override
    public void create(CreateBo bo) {

        Date date = new Date(System.currentTimeMillis());

        // goods
        Goods goods = bo.getGoods();
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        goodsMapper.insertSelective(goods);

        // attributes
        List<GoodsAttribute> attributes = bo.getAttributes();
        for (GoodsAttribute attribute : attributes) {
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            attributeMapper.insertSelective(attribute);
        }

        // products
        List<GoodsProduct> products = bo.getProducts();
        for (GoodsProduct product : products) {
            product.setAddTime(date);
            product.setUpdateTime(date);
            productMapper.insertSelective(product);
        }

        // specifications
        List<GoodsSpecification> specifications = bo.getSpecifications();
        for (GoodsSpecification specification : specifications) {
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            specificationMapper.insertSelective(specification);
        }

    }

}
